package com.small.nine.wxmp.controller;

import com.small.nine.wxmp.domain.bean.wx.WxInMsgBean;
import com.small.nine.wxmp.domain.bean.wx.WxMenuButtonBean;
import com.small.nine.wxmp.domain.bean.wx.kf.WxKfAccountBean;
import com.small.nine.wxmp.domain.bean.wx.qrCode.WxTicketBean;
import com.small.nine.wxmp.domain.bean.wx.template.WxTemplateIdBean;
import com.small.nine.wxmp.domain.bean.wx.template.WxTemplateIndustryBean;
import com.small.nine.wxmp.domain.bean.wx.template.WxTemplateListBean;
import com.small.nine.wxmp.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 微信公众号相关服务 API
 *
 * @author sheng_zs@126.com
 * @date 2021-07-26 9:07
 */
@Slf4j
@RestController
@RequestMapping("/wx")
public class WxController {
    @Resource
    private WxService wxService;

    /**
     * 微信接入，验证是否是微信发送的信息
     *
     * @param signature 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * @param timestamp 时间戳
     * @param nonce     随机数
     * @param echoStr   随机字符串
     * @return 验证成功返回 echoStr
     */
    @GetMapping("/weChat")
    public String checkSignature(@RequestParam String signature, @RequestParam String timestamp,
                                 @RequestParam String nonce, @RequestParam("echostr") String echoStr) {
        return wxService.checkSignature(signature, timestamp, nonce, echoStr);
    }

    /**
     * 接收微信公众号关注者的消息并且给予回应<br>
     * 返回 xml 格式<br>
     * 路径与`测试号 --> 接口配置信息 --> URL`一致，即与`微信接入`的`URL`一致，但为 `POST` 请求
     * <p>还有一个参数：消息发送者 openId，@RequestParam(required = false) String openid，请求体里面有了，且如果加密，该参数为空</p>
     *
     * @param bean         {@link WxInMsgBean}，公众号关注者发送的消息
     * @param signature    加密后的字符串
     * @param timestamp    时间戳
     * @param nonce        随机数
     * @param encType      "aes"，加密类型
     * @param msgSignature token、timestamp、nonce及密文经过 AES 加密后的签名字符串
     * @return 返回 XMl 字符串
     */
    @PostMapping(value = "/weChat", produces = "application/xml; charset=UTF-8")
    public String msgHandle(@RequestBody WxInMsgBean bean,
                            @RequestParam(required = false) String signature,
                            @RequestParam(required = false) String timestamp,
                            @RequestParam(required = false) String nonce,
                            @RequestParam(name = "encrypt_type", required = false) String encType,
                            @RequestParam(name = "msg_signature", required = false) String msgSignature) {
        return wxService.msgHandle(bean, signature, timestamp, nonce, encType, msgSignature);
    }

    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        return wxService.getAccessToken();
    }

    /**
     * <p>
     *    [
     *     {"type": "click","name": "点赞", "key": "key_001"},
     *     {
     *         "name": "笔记",
     *         "sub_button"/"subButtons": [
     *             {"type": "view","name": "简书","url": "https://www.jianshu.com/"},
     *             {"type": "view","name": "CSDN","url": "https://blog.csdn.net/"},
     *             {"type": "view","name": "有道云笔记","url": "https://note.youdao.com/signIn/index.html"}
     *         ]
     *     }
     *    ]
     * </p>
     *
     * @param beans <p>List<WxMenuButtonBean></p>，格式如上
     * @return 微信返回信息
     */
    @PostMapping("/createMenu")
    public Map<String, Object> createMenu(@RequestBody List<WxMenuButtonBean> beans) {
        return wxService.createMenu(beans);
    }

    @PostMapping("/deleteMenu")
    public Map<String, Object> deleteMenu() {
        return wxService.deleteMenu();
    }

    @PostMapping("/addCustom")
    public Map<String, Object> addCustom(@RequestBody WxKfAccountBean bean) {
        return wxService.addCustom(bean);
    }

    @PostMapping("/delCustom")
    public Map<String, Object> delCustom(@RequestParam String account) {
        return wxService.delCustom(account);
    }

    @GetMapping("/getAllKf")
    public List<WxKfAccountBean> getAllKf() {
        return wxService.getKfList();
    }

    @GetMapping("/getTemporaryQrCode")
    public WxTicketBean getTemporaryQrCode() {
        return wxService.createTemporaryQrCode();
    }

    @GetMapping("/getQrCodeImage")
    public void getQrCodeImage(HttpServletResponse response) {
        wxService.getQrCode(response);
    }

    /**
     * 设置行业
     *
     * @param primaryIndustry   主行业
     * @param secondaryIndustry 副行业
     * @return {@link Map} 微信服务器返回信息
     */
    @PostMapping("/setIndustry")
    public Map<String, Object> setIndustry(@RequestParam String primaryIndustry, @RequestParam String secondaryIndustry) {
        return wxService.setIndustry(primaryIndustry, secondaryIndustry);
    }

    /**
     * 获取行业
     *
     * @return 1 成功
     */
    @GetMapping("/getIndustry")
    public WxTemplateIndustryBean getIndustry() {
        return wxService.getIndustry();
    }

    @GetMapping("/getAllTemplate")
    public WxTemplateListBean getAllTemplate() {
        return wxService.getAllTemplate();
    }

    @PostMapping("/addTemplate")
    public WxTemplateIdBean addTemplate(@RequestParam String shortId) {
        return wxService.addTemplate(shortId);
    }

    /**
     * 发送模板消息
     */
    @PostMapping("/sendTemplateMsg")
    public Map<String, Object> sendTemplateMsg() {
        return wxService.sendTemplateMsg();
    }
}
